// 删除已存在的约束
DROP CONSTRAINT constraint_153ebae IF EXISTS;
DROP CONSTRAINT character_id IF EXISTS;
DROP CONSTRAINT weapon_id IF EXISTS;
DROP CONSTRAINT skill_id IF EXISTS;
DROP CONSTRAINT location_id IF EXISTS;

// 创建索引
CREATE CONSTRAINT character_id IF NOT EXISTS FOR (c:Character) REQUIRE c.id IS UNIQUE;
CREATE CONSTRAINT weapon_id IF NOT EXISTS FOR (w:Weapon) REQUIRE w.id IS UNIQUE;
CREATE CONSTRAINT skill_id IF NOT EXISTS FOR (s:Skill) REQUIRE s.id IS UNIQUE;
CREATE CONSTRAINT location_id IF NOT EXISTS FOR (l:Location) REQUIRE l.id IS UNIQUE;

// 创建角色节点
RETURN 'Creating Character nodes';
LOAD CSV with headers from "file:///Sample-CSV/character_new.csv" as line
CALL {
    with line
    //CREATE (n:Character
    MERGE (n:Character
        { nodetype: 'character',
          id: line.id,
          active: line.active,
          effectiveTime: line.effectiveTime,
          moduleId: line.moduleId,
          FSN: line.FSN,
          history: line.history} )
} IN TRANSACTIONS OF 200 ROWS;

// 创建武器节点
RETURN 'Creating Weapon nodes';
LOAD CSV with headers from "file:///Sample-CSV/weapon_new.csv" as line
CALL {
    with line
    //CREATE (n:Weapon
    MERGE (n:Weapon
        { nodetype: 'weapon',
          id: line.id,
          active: line.active,
          effectiveTime: line.effectiveTime,
          moduleId: line.moduleId,
          FSN: line.FSN,
          history: line.history} )
} IN TRANSACTIONS OF 200 ROWS;

// 创建技能节点
RETURN 'Creating Skill nodes';
LOAD CSV with headers from "file:///Sample-CSV/skill_new.csv" as line
CALL {
    with line
    //CREATE (n:Skill
    MERGE (n:Skill
        { nodetype: 'skill',
          id: line.id,
          active: line.active,
          effectiveTime: line.effectiveTime,
          moduleId: line.moduleId,
          FSN: line.FSN,
          history: line.history} )
} IN TRANSACTIONS OF 200 ROWS;

// 创建地点节点
RETURN 'Creating Location nodes';
LOAD CSV with headers from "file:///Sample-CSV/location_new.csv" as line
CALL {
    with line
    //CREATE (n:Location
    MERGE (n:Location
        { nodetype: 'location',
          id: line.id,
          active: line.active,
          effectiveTime: line.effectiveTime,
          moduleId: line.moduleId,
          FSN: line.FSN,
          history: line.history} )
} IN TRANSACTIONS OF 200 ROWS;

// 创建HAS_WEAPON关系
RETURN 'Creating HAS_WEAPON relationships';
LOAD CSV with headers from "file:///Sample-CSV/DR_has_weapon_new.csv" as line
CALL {
    with line
    MATCH (c:Character { id: line.sourceId }), (w:Weapon { id: line.destinationId })
    MERGE (c)-[:HAS_WEAPON { 
        id: line.id,
        active: line.active,
        effectiveTime: line.effectiveTime,
        moduleId: line.moduleId,
        characteristicTypeId: line.characteristicTypeId,
        modifierId: line.modifierId,
        history: line.history,
        rolegroup: line.rolegroup
    }]->(w)
} IN TRANSACTIONS OF 200 ROWS;

// 创建HAS_SKILL关系
RETURN 'Creating HAS_SKILL relationships';
LOAD CSV with headers from "file:///Sample-CSV/DR_has_skill_new.csv" as line
CALL {
    with line
    MATCH (c:Character { id: line.sourceId }), (s:Skill { id: line.destinationId })
    MERGE (c)-[:HAS_SKILL { 
        id: line.id,
        active: line.active,
        effectiveTime: line.effectiveTime,
        moduleId: line.moduleId,
        characteristicTypeId: line.characteristicTypeId,
        modifierId: line.modifierId,
        history: line.history,
        rolegroup: line.rolegroup
    }]->(s)
} IN TRANSACTIONS OF 200 ROWS;

// 创建LOCATED_IN关系
RETURN 'Creating LOCATED_IN relationships';
LOAD CSV with headers from "file:///Sample-CSV/DR_located_in_new.csv" as line
CALL {
    with line
    MATCH (c:Character { id: line.sourceId }), (l:Location { id: line.destinationId })
    MERGE (c)-[:LOCATED_IN { 
        id: line.id,
        active: line.active,
        effectiveTime: line.effectiveTime,
        moduleId: line.moduleId,
        characteristicTypeId: line.characteristicTypeId,
        modifierId: line.modifierId,
        history: line.history,
        rolegroup: line.rolegroup
    }]->(l)
} IN TRANSACTIONS OF 200 ROWS; 